<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="roleList" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom',index === 0 ? 'bdtop' :'','vcenter']"
              v-for="(item,index) in scope.row.children"
              :key="item.id"
            >
              <el-col :span="5">
                <el-tag @close="onRemoveById(scope.row,item.id)" closable>{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  :class="[index1 === 0 ? '' : 'bdtop','vcenter']"
                  v-for="(item1,index1) in item.children"
                  :key="item1.id"
                >
                  <el-col :span="6">
                    <el-tag
                      @close="onRemoveById(scope.row,item1.id)"
                      closable
                      type="success"
                    >{{item1.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="(item2) in item1.children"
                      :key="item2.id"
                      type="warning"
                      closable
                      @close="onRemoveById(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button
              @click="onShowSetFightsDialog(scope.row)"
              type="warning"
              icon="el-icon-setting"
              size="mini"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="分配权限"
        :visible.sync="isSetFightsDialog"
        width="50%"
        @close="closeSetFightsDialog"
        ref="fightsRef"
      >
        <el-tree
          show-checkbox
          :data="rightsList"
          :props="treeProps"
          node-key="id"
          default-expand-all
          :default-checked-keys="defaultKeys"
          @node-click="handleNodeClick"
          ref="treeRef"
        ></el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="isSetFightsDialog = false">取 消</el-button>
          <el-button type="primary" @click="onSetRoleRights">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      roleList: null,
      isSetFightsDialog: false,
      rightsList: null,
      editRoleID: null,
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      defaultKeys: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 关闭分配权限对话框
    closeSetFightsDialog() {
      this.isSetFightsDialog = false
      this.editRoleID = null
      this.defaultKeys = []
    },
    handleNodeClick() {},
    // 获取
    onRemoveById(role, rightsid) {
      console.log(role, rightsid)
      this.$confirm('是否确定删除该标签', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          this.reomveTag(role, rightsid)
        })
        .catch(() => {
          this.$message.error('已取消删除')
        })
    },
    // 显示设置对话框
    onShowSetFightsDialog(role) {
      console.log(role)
      this.getLeafKeys(role, this.defaultKeys)
      this.editRoleID = role.id
      this.isSetFightsDialog = true
      this.getRights()
    },
    // 通过递归的形式，获取角色下的所有三级ID
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    // 设置角色权限
    onSetRoleRights() {
      let keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      let idStr = keys.join(',')
      this.setRoleRights(idStr)
    },
    async setRoleRights(idStr) {
      const { data: res } = await this.$http.post(
        `roles/${this.editRoleID}/rights`,
        {
          rids: idStr
        }
      )
      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败')
      }
      this.$message.success('分配权限成功')
      this.getRoleList()
      this.isSetFightsDialog = false
    },
    async getRights() {
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$message.error('获取权限列表失败')
      }
      console.log(res)
      this.rightsList = res.data
    },
    async reomveTag(role, rightsid) {
      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightsid}`
      )
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      role.children = res.data
      // this.getRoleList()
    },
    // 获取角色列表
    async getRoleList() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
      this.roleList = res.data
    }
  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.vcenter {
  display: flex;
  align-items: center;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
</style>
